package com.blustreaksw.cyclecoach.main;

import java.util.ArrayList;
import java.util.Calendar;

import util.DatePickerFragment;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;

import com.blustreaksw.cyclecoach.R;

public class CreateProfileActivity extends FragmentActivity {
	
	public enum USER_GENDER {
		UNKNOWN,
		MALE,
		FEMALE
	};
	
	ArrayAdapter<String> mHeightSpinnerAdapter;
	ArrayAdapter<String> mWeightSpinnerAdapter;
	ArrayAdapter<CharSequence> mGenderSpinnerAdapter;
	private Button mDatePicker_dateOfBirth;
	private Calendar mDateOfBirth;
	private DatePickerFragment mDatePickerFragment;
	private EditText mUserNameEditText;
	private Spinner mHeightSpinner;
	private Spinner mWeightSpinner;
	private Spinner mGenderSpinner;
	private int mHeight;
	private int mWeight;
	private USER_GENDER mGender;
	private String mUserName;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_user);
		
		getActionBar().setSubtitle("  Create Profile");
		
		/* Point to the Date of Birth selector and configure
		 */
		mDatePickerFragment = new DatePickerFragment();
		mDateOfBirth = Calendar.getInstance();
		mDateOfBirth.set(1980, 0, 1);
		mDatePicker_dateOfBirth = (Button)findViewById(R.id.createProfileActivity_dateOfBirthButton);
		mDatePicker_dateOfBirth.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mDatePickerFragment.configureDatePicker(mOnDateSetListener, mDateOfBirth);
				mDatePickerFragment.show(getSupportFragmentManager(), null);
			}
			
		});
		
		mDatePickerFragment.updateDateOfBirthButtonText(mDatePicker_dateOfBirth, mDateOfBirth);
		
		/* Set link to the user name EditText
		 * 
		 */
		mUserNameEditText = (EditText)findViewById(R.id.createProfileActivity_userNameEditText);
		
		/* Set link to the height spinner and configure.
		 * 
		 */
		mHeightSpinner = (Spinner)findViewById(R.id.createProfileActivity_heightSpinner);
		configureHeightSpinner(false);
		
		mWeightSpinner = (Spinner)findViewById(R.id.createProfileActivity_weightSpinner);
		configureWeightSpinner(false);
		
		mGenderSpinner = (Spinner)findViewById(R.id.createProfileActivity_genderSpinner);
		configureGenderSpinner();
		
		mUserNameEditText = (EditText)findViewById(R.id.createProfileActivity_userNameEditText);
		/*mUserNameEditText.addTextChangedListener(new TextWatcher () {

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				mUserName = new String(mUserNameEditText.getText().toString());
				Log.d("mUserNameEditText afterTextChanged()", "mUserName = " + mUserName);
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				
			}
			
		});*/
		mUserNameEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(!hasFocus) {
					mUserName = new String(mUserNameEditText.getText().toString());
					Log.d("mUserNameEditText afterTextChanged()", "mUserName = " + mUserName);
				}
			}
			
		});
		
	}
	
	OnDateSetListener mOnDateSetListener = new OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			mDateOfBirth.set(year, monthOfYear, dayOfMonth);
			mDatePickerFragment.updateDateOfBirthButtonText(mDatePicker_dateOfBirth, mDateOfBirth);
		}
		
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu m) {
		getMenuInflater().inflate(R.menu.menu_wizard, m);
		
		MenuItem menuItem_back = m.findItem(R.id.wizard_menuItem_back);
		menuItem_back.setVisible(false);
		
		m.findItem(R.id.wizard_menuItem_back).setVisible(false);
		m.findItem(R.id.wizard_menuItem_forward).setVisible(false);
		
		
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle presses on action bar items
		switch(item.getItemId()) {
			case R.id.wizard_menuItem_cancel:
				startActivity(new Intent(this, SelectProfileActivity.class));
				return true;
				
			case R.id.wizard_menuItem_save:
				startActivity(new Intent(this, MainActivity.class));
				
				return true;
			
			default:
				return super.onOptionsItemSelected(item);
		}
	}
	
	public void configureHeightSpinner(boolean metric) {
		ArrayList<String> heightList = new ArrayList<String>();
		if(metric) {
			for(int i = 90; i < 225; i++) {
				heightList.add(Integer.toString(i) + " cm");
			}
			mHeightSpinnerAdapter = new ArrayAdapter<String>(
					this, android.R.layout.simple_spinner_item, heightList);
			mHeightSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> parent, View view,
						int pos, long id) {
					mHeight = pos + 89;
					Log.d("mHeightSpinner onItemSelected()", "mHeight = " + Integer.toString(mHeight));
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			
			mHeightSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
			mHeightSpinner.setAdapter(mHeightSpinnerAdapter);
			
			// Set the default height to 172 cm
			mHeightSpinner.setSelection(82);
		}
		else {
			for(int i = 36; i < 90; i++) {
				heightList.add(Integer.toString(i/12) + "\' " + Integer.toString(i%12) + "\"");
			}
			mHeightSpinnerAdapter = new ArrayAdapter<String>(
					this, android.R.layout.simple_spinner_item, heightList);
			mHeightSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> parent, View view,
						int pos, long id) {
					
					mHeight = (int) Math.ceil((pos + 36) * 2.54);
					Log.d("mHeightSpinner onItemSelected()", "mHeight = " + Integer.toString(mHeight));
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			
			mHeightSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
			mHeightSpinner.setAdapter(mHeightSpinnerAdapter);
			
			// Set the default height to 66 inches.
			mHeightSpinner.setSelection(30);
		}
	}
	
	public void configureWeightSpinner(boolean metric) {
		ArrayList<String> weightList = new ArrayList<String>();
		if(metric) {
			
			for(int i = 0; i < 205; i++) {
				weightList.add(Integer.toString(i) + " kg");
			}
			
			mWeightSpinnerAdapter = new ArrayAdapter<String>(
					this, android.R.layout.simple_spinner_item, weightList);
			mWeightSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> parent, View view,
						int pos, long id) {
					mWeight = pos;
					Log.d("mWeightSpinner onItemSelected()", "mWeight = " + Integer.toString(mWeight));
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			
			mWeightSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
			mWeightSpinner.setAdapter(mWeightSpinnerAdapter);
			
			// Set default weight to 50 kg
			mWeightSpinner.setSelection(50);
		}
		else {
			for(int i = 0; i < 450; i++) {
				weightList.add(Integer.toString(i) + " lbs");
			}
			
			mWeightSpinnerAdapter = new ArrayAdapter<String>(
					this, android.R.layout.simple_spinner_item, weightList);
			mWeightSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> parent, View view,
						int pos, long id) {
					mWeight = (int) Math.ceil(pos * 2.2);
					Log.d("mWeightSpinner onItemSelected()", "mWeight = " + Integer.toString(mWeight));
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			
			mWeightSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
			mWeightSpinner.setAdapter(mWeightSpinnerAdapter);
			
			// Set default weight to 100 lbs
			mWeightSpinner.setSelection(100);
		}
	}
	
	public void configureGenderSpinner() {
	
		mGenderSpinnerAdapter =  ArrayAdapter.createFromResource(
				this, R.array.createProfile_genderList, android.R.layout.simple_spinner_item);
		mGenderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				switch(pos) {
					case 1:
						mGender = USER_GENDER.MALE;
						break;
					case 2:
						mGender = USER_GENDER.FEMALE;
						break;
					default:
						mGender = USER_GENDER.UNKNOWN;
						break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				mGender = USER_GENDER.UNKNOWN;
			}
		});
		
		mGenderSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
		mGenderSpinner.setAdapter(mGenderSpinnerAdapter);
		
	}
}
